package com.ssyc.gsk_teacher.activity;

import android.content.Context;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.manager.StateBarManager;
import com.ssyc.common.utils.WebViewUtils;
import com.ssyc.gsk_teacher.R;

/**
 * 老师题库
 * Created by Kevin on 2018/8/10.
 */

public class TeacherPracticeActivity extends BaseActivity {

    private WebView webView;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.base_activity_tk;
    }

    @Override
    protected void init() {

        showContent();

        //配置状态栏颜色
        initStateColor();

        initView();

        setWebView();
    }

    /**
     * 配置状态栏颜色的方法
     */
    private void initStateColor() {
        //设置6.0以后沉浸状体栏的问题
        StateBarManager.setBarWithUltimateBar(this, getResources().getColor(com.ssyc.common.R.color.tkColor),
                getResources().getColor(com.ssyc.common.R.color.nav_color));
    }

    /**
     * 初始化视图
     */
    private void initView() {
        webView = findView(R.id.webView);
    }

    /**
     * 设置Webview
     */
    private void setWebView() {
        WebViewUtils.setWebview(this, webView);

        webView.setWebViewClient(new myWebViewClient());

        webView.addJavascriptInterface(new WebAppInterface(this), "Android");

        //题库正式
        webView.loadUrl("https://moregolden.com:36357/tkNewPageTest/teacher-index.html?v=1"
                +System.currentTimeMillis());

//        外网测试服
//      webView.loadUrl("https://moregolden.com:36357/tkNewPageDemo/teacher-index.html");

    }

    public class myWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            webView.loadUrl("javascript:alertName('" + AccountUtils.getAccount(TeacherPracticeActivity.this)
                    + "','" + AccountUtils.getName(TeacherPracticeActivity.this) + "')");
        }
    }



    public class WebAppInterface {
        Context mContext;

        /**
         * Instantiate the interface and set the context
         */
        WebAppInterface(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void showToast(String toast) {
            finish();
        }

        @JavascriptInterface
        public void finishActivity() {
            finish();
        }

        @JavascriptInterface
        public void IntentButtonActivity() {
            finish();
        }

        @JavascriptInterface
        public void ExitLogin() {
            finish();
        }

        // 传递参数
        @JavascriptInterface
        public String getParam() {
            String param;
            param = AccountUtils.getAccount(TeacherPracticeActivity.this);
            Log.e("test", "param=" + param);
            return param;
        }

        // 传递参数
        @JavascriptInterface
        public String getType() {
            return "1";
        }

        @JavascriptInterface
        public void shareResults(String scoreInfo) {
            //moduleType=1&userType=0&time=41&mark=0&num=18271261779

//            Intent intent = new Intent(mContext, ShareActivity.class);
//            intent.putExtra("scoreInfo", scoreInfo);
//            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.clearCache(true);
        webView.loadUrl("about:blank");
        webView.stopLoading();
        webView.setWebChromeClient(null);
        webView.setWebViewClient(null);
        webView.destroy();
        webView = null;

    }
}
